import { ref, computed } from 'vue'
import { sendCaptcha } from '@r/api/user'
import { message } from 'ant-design-vue'
import regular from '@/utils/regular'
const CountNumber = 60
function useSendCode() {
  let countCurrent = ref(CountNumber)
  let codeLoading = ref(false)
  const handleGetCode = (mobilePhone: string | undefined) => {
    console.log(111111);
    if (!mobilePhone || !regular.regPhone.test(mobilePhone)) {
      message.warn('请输入正确的手机号')
      return
    }
    handleCountDown()
    console.log('发送成功')
    sendCaptcha({
      mobilePhone,
      // captchaType: 1
    })
      .then(() => {
        message.success('发送成功')
      })
      .catch(() => { })
  }

  const handleCountDown = () => {
    if (countCurrent.value === 0) {
      countCurrent.value = CountNumber
      return
    }
    countCurrent.value = countCurrent.value - 1

    setTimeout(() => {
      handleCountDown()
    }, 1000)
  }
  return {
    CountNumber,
    codeLoading,
    countCurrent,
    handleGetCode
  }
}
export {
  useSendCode
}